package oop.core.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import oop.core.entity.Avatar;
import oop.core.entity.Entity;
import oop.core.item.ObstacleItem;
import oop.core.item.TakeableItem;
import oop.core.map.Decal;
import oop.core.map.GameMap;
import oop.core.map.Location;
import oop.core.map.Terrain;
import oop.core.skill.SkillObject;
import oop.core.visitors.InteractiveItemVisitable;

/**
 * AreaPanel is the part of the panel that holds the map.
 * 
 *
 */

public class AreaPanel extends SizeablePanel {

	private final static Dimension SIZE = new Dimension(600, 600);
	private final static Point LOCATION = new Point(0, 0);
	
	private GameMap map;
	
	public AreaPanel()
	{
		super(LOCATION, SIZE);
	}
	
	public void takeMap(GameMap map)
	{
		this.map = map;
	}
	
	/**
	 * Draws the map on this panel by going through each list
	 * and determing where things are located.
	 */
	protected void paintComponent(Graphics g)
	{
//		System.out.println("Repainting AreaPanel");
		g.setColor(Color.MAGENTA);
		g.fillRect(0, 0, (int)SIZE.getWidth(), (int)SIZE.getHeight());
		
//		System.out.println("Painting AreaPanel");
		int w = (int)SIZE.getWidth()/40;
		int h = (int)SIZE.getHeight()/40;
		
		List<Entity> entityList = map.getEntityList();
		Map<Location, Terrain> terrainMap = map.getTerrainMap();
		
		
		// At times like this I wish I could make a List< Map<Location, Renderable> >
		Map<Location, Decal> decalMap = map.getDecalMap();
		Map<Location, TakeableItem> takeableItemMap = map.getTakeableItemMap();
		Map<Location, ObstacleItem> obstacleItemMap = map.getObstacleItemMap();
		Map<Location, InteractiveItemVisitable> interactiveItemMap = map.getInteractiveItemVisitableMap();
		Map<Location, SkillObject> skillObjectMap = map.getSkillObjectMap();
		
		Avatar avatar = map.getPlayer();
		
		int startX = avatar.getLoc().getXPos()-w/2;
		int endX = avatar.getLoc().getXPos()+w/2;
		int startY = avatar.getLoc().getYPos()-h/2;
		int endY = avatar.getLoc().getYPos()+h/2;

//		System.out.println("(startX, endX) = (" + startX + "," + endX + ")");
//		System.out.println("(startY, endY) = (" + startY + "," + endY + ")");
//		System.out.println("Avatar location: (" + avatar.getLoc().getXPos() + "," + avatar.getLoc().getYPos() + ")");
		

//		System.out.println("x: (" + startX + "," + endX + ")");
//		System.out.println("y: (" + startY + "," + endY + ")");
		for(int x=startX; x<=endX; x++)
		{
			for(int y=startY; y<=endY; y++)
			{
				Location location = new Location(x,y);
				
				// Order of rendering: Terrain, Decal, TakeableItem, ObstacleItem, InteractiveItem, SkillObject
				Terrain terrain = terrainMap.get(location);
				g.drawImage(ImageHandler.getImage(terrain.getImageID()), (x-startX)*40, (y-startY)*40, null);

				if(decalMap != null) {
					Decal decal = decalMap.get(location);
					if(decal != null)
						g.drawImage(ImageHandler.getImage(decal.getImageID()), (x-startX)*40, (y-startY)*40, null);
				}
				
				if(takeableItemMap != null) {
					TakeableItem takeableItem = takeableItemMap.get(location);
					if(takeableItem != null)
						g.drawImage(ImageHandler.getImage(takeableItem.getImageID()), (x-startX)*40, (y-startY)*40, null);
				}
				
				if(obstacleItemMap != null) {
					ObstacleItem obstacleItem = obstacleItemMap.get(location);
					if(obstacleItem != null)
						g.drawImage(ImageHandler.getImage(obstacleItem.getImageID()), (x-startX)*40, (y-startY)*40, null);
				}
				
				if(interactiveItemMap != null) {
					InteractiveItemVisitable interactiveItem = interactiveItemMap.get(location);
					if(interactiveItem != null){
						g.drawImage(ImageHandler.getImage(interactiveItem.getImageID()), (x-startX)*40, (y-startY)*40, null);
					}
				}
				
				if(skillObjectMap != null) {
					SkillObject skillObject = skillObjectMap.get(location);
					if(skillObject != null)
						g.drawImage(ImageHandler.getImage(skillObject.getImageID()), (x-startX)*40, (y-startY)*40, null);
				}
			}
		}
		
		if(entityList != null) {
			for(Entity entity : entityList)
			{
				Location location = entity.getLoc();
				int x = location.getXPos();
				int y = location.getYPos();
				if(x >= startX && x <= endX && y >= startY && y <= endY)
					g.drawImage(ImageHandler.getImage(entity.getImageID()), (x-startX)*40, (y-startY)*40, null);
			}
		}
		
		g.drawImage(ImageHandler.getImage(avatar.getImageID()), w/2*40, h/2*40,null);
	}
}
